﻿using CCWin;
using DC.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;


namespace DC.Views
{
    public partial class Form_Process : Skin_DevExpress
    {
        Utils.Database DB = Utils.Global.GlobalDB;
        public string processID = string.Empty;
        public string patientID = string.Empty;
        public int process_Judge = 0; // 1为新建 2为修改
        public Form_Process()
        {
            InitializeComponent();
        }

        private void Form_Process_Load(object sender, EventArgs e)
        {
            LoadPatientInfo();
            LoadColorList();
            LoadTypeList();
            LoadFirmList();
            LoadUserList();
            LoadSenderName();
            if (process_Judge == 1)
            {
                skinComboBox1.SelectedIndex = -1;
                skinComboBox2.SelectedIndex = -1;
                skinComboBox3.SelectedIndex = -1;
                skinComboBox4.SelectedIndex = -1;
                skinComboBox5.SelectedIndex = -1;
                skinComboBox6.SelectedIndex = -1;
                skinComboBox7.SelectedIndex = -1;
                skinComboBox8.SelectedIndex = -1;
                this.Text = "新建加工单";
            }
            else if (process_Judge == 2)
            {
                this.Text = "修改加工单";
                LoadHistoryProcess();
            }

        }

        private void LoadHistoryProcess()
        {
            string sql = @"select Process_NoteNumber,Process_Firm,Process_SendTime,
                                  Process_SenderPhone,Process_GetTime,Process_SenderName,Process_LocationInfo,[User].Real_Name,Process_Status,Process_Remark
                                  FROM　Process,[User] WHERE Process_ID='" + processID + "'";
            DataView ProcessInfo = DB.GetDs(sql).Tables[0].DefaultView;
            skinComboBox9.Text = ProcessInfo[0][1].ToString();
            skinComboBox10.Text = ProcessInfo[0][5].ToString();
            textBoxEx2.Text = ProcessInfo[0][3].ToString();
            skinComboBox11.Text = ProcessInfo[0][7].ToString();
            dateTimePicker1.Value = Convert.ToDateTime(ProcessInfo[0][2].ToString());
            dateTimePicker2.Value = Convert.ToDateTime(ProcessInfo[0][4].ToString());
            skinComboBox12.Text = ProcessInfo[0][8].ToString();
            textBoxEx1.Text = ProcessInfo[0][0].ToString();
            ucTextBoxEx1.InputText = ProcessInfo[0][9].ToString();

            string Locationinfo = ProcessInfo[0][6].ToString();
            string[] item = Locationinfo.Split(';');
            string[] Location_Division1 = item[0].Split(',');
            textBox1.Text = Location_Division1[0];
            textBox2.Text = Location_Division1[1];
            textBox3.Text = Location_Division1[2];
            textBox4.Text = Location_Division1[3];
            skinComboBox1.Text = Location_Division1[4];
            skinComboBox5.Text = Location_Division1[5];

            string[] Location_Division2 = item[1].Split(',');
            textBox8.Text = Location_Division2[0];
            textBox7.Text = Location_Division2[1];
            textBox6.Text = Location_Division2[2];
            textBox5.Text = Location_Division2[3];
            skinComboBox2.Text = Location_Division2[4];
            skinComboBox6.Text = Location_Division2[5];

            string[] Location_Division3 = item[2].Split(',');
            textBox12.Text = Location_Division3[0];
            textBox11.Text = Location_Division3[1];
            textBox10.Text = Location_Division3[2];
            textBox9.Text = Location_Division3[3];
            skinComboBox3.Text = Location_Division3[4];
            skinComboBox7.Text = Location_Division3[5];

            string[] Location_Division4 = item[3].Split(',');
            textBox16.Text = Location_Division4[0];
            textBox15.Text = Location_Division4[1];
            textBox14.Text = Location_Division4[2];
            textBox13.Text = Location_Division4[3];
            skinComboBox4.Text = Location_Division4[4];
            skinComboBox8.Text = Location_Division4[5];
        }

        private void LoadSenderName()
        {
            skinComboBox10.Items.Clear();
            DataView SenderList = DB.GetDs("select distinct(Process_SenderName) FROM Process").Tables[0].DefaultView;
            skinComboBox10.DisplayMember = "Process_SenderName";
            skinComboBox10.DataSource = SenderList;
        }

        private void LoadUserList()
        {
            skinComboBox11.DataSource = null;
            List<string> UserList = new List<string>();
            DataView UserView = DB.GetDs("select User_ID,Real_Name FROM [User]").Tables[0].DefaultView;

            skinComboBox11.ValueMember = "User_ID";
            skinComboBox11.DisplayMember = "Real_Name";
            skinComboBox11.DataSource = UserView;
            skinComboBox11.SelectedIndex = -1;
        }

        /// <summary>
        /// 加载加工单位下拉表
        /// </summary>
        private void LoadFirmList()
        {
            List<string> FirmList = new List<string>();
            skinComboBox9.DataSource = null;
            string FilePath = Application.StartupPath + "\\config\\ProcessFirm.bin";

            if (!File.Exists(FilePath))
            {
                File.Create(FilePath);
            }

            foreach (string item in FileHelper.ReadFile(FilePath).Split('\n'))
            {
                FirmList.Add(item);
            }
            skinComboBox9.DataSource = FirmList;
            skinComboBox9.SelectedIndex = -1;

        }

        /// <summary>
        ///  加载加工类型
        /// </summary>
        private void LoadTypeList()
        {
            List<string> TypeList = new List<string>();

            string FilePath = Application.StartupPath + "\\config\\ProcessType.bin";

            if (!File.Exists(FilePath))
            {
                File.Create(FilePath);
            }

            skinComboBox1.Items.Clear();
            skinComboBox2.Items.Clear();
            skinComboBox3.Items.Clear();
            skinComboBox4.Items.Clear();
            foreach (string item in FileHelper.ReadFile(FilePath).Split('\n'))
            {
                skinComboBox1.Items.Add(item);
                skinComboBox2.Items.Add(item);
                skinComboBox3.Items.Add(item);
                skinComboBox4.Items.Add(item);
            }
        }
        /// <summary>
        /// 加载色号Combox
        /// </summary>
        private void LoadColorList()
        {
            List<string> ColorList = new List<string>();

            string FilePath = Application.StartupPath + "\\config\\ProcessColor.bin";

            if (!File.Exists(FilePath))
            {
                File.Create(FilePath);
            }
            skinComboBox5.Items.Clear();
            skinComboBox6.Items.Clear();
            skinComboBox7.Items.Clear();
            skinComboBox8.Items.Clear();
            foreach (string item in FileHelper.ReadFile(FilePath).Split('\n'))
            {
                skinComboBox5.Items.Add(item);
                skinComboBox6.Items.Add(item);
                skinComboBox7.Items.Add(item);
                skinComboBox8.Items.Add(item);
            }
        }

        private void LoadPatientInfo()
        {
            DataView PatientInfo = DB.GetDs("select Patient_Name,Patient_Sex from Patient WHERE Patient_ID ='" + patientID + "'").Tables[0].DefaultView;
            label1.Text = "姓名：" + PatientInfo[0][0].ToString() + " 性别：" + PatientInfo[0][1].ToString();
        }

        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
            if (process_Judge == 1)
            {
                InsertNewProcess();
            }
            else if (process_Judge == 2)
            {
                ModifyOldProcess();
            }
            this.Close();
        }

        private void ModifyOldProcess()
        {
            if (skinComboBox9.Text == string.Empty || skinComboBox10.Text == string.Empty || skinComboBox11.Text == string.Empty || skinComboBox12.Text == string.Empty
                 || textBoxEx1.Text == string.Empty || textBoxEx2.Text == string.Empty)
            {
                MessageBox.Show("填写信息不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string TeethLocation;
            TeethLocation = textBox1.Text + "," + textBox2.Text + "," + textBox3.Text + "," + textBox4.Text + "," + skinComboBox1.Text + "," + skinComboBox5.Text + ";"
                       + textBox8.Text + "," + textBox7.Text + "," + textBox6.Text + "," + textBox5.Text + "," + skinComboBox2.Text + "," + skinComboBox6.Text + ";"
                       + textBox12.Text + "," + textBox11.Text + "," + textBox10.Text + "," + textBox9.Text + "," + skinComboBox3.Text + "," + skinComboBox7.Text + ";"
                       + textBox16.Text + "," + textBox15.Text + "," + textBox14.Text + "," + textBox13.Text + "," + skinComboBox4.Text + "," + skinComboBox8.Text + ";";

            string sql = @"update Process set Process_NoteNumber='" + textBoxEx1.Text +
                                              "' ,Process_Firm='" + skinComboBox9.Text +
                                              "' ,Process_SendTime='" + dateTimePicker1.Value.ToString("yyyy-MM-dd") +
                                              "' ,Process_SenderPhone='" + textBoxEx2.Text +
                                              "' ,Process_GetTime='" + dateTimePicker2.Value.ToString("yyyy-MM-dd") +
                                              "' ,Process_SenderName='" + skinComboBox10.Text +
                                              "' ,Process_LocationInfo='" + TeethLocation +
                                              "' ,User_ID=" + skinComboBox11.SelectedValue +
                                              "  ,Process_Status='" + skinComboBox12.Text +
                                              "' ,Process_Remark='" + ucTextBoxEx1.InputText + "' WHERE Process_ID='"+processID+"'";

            DB.Execute(sql);
            MessageBox.Show("修改成功", "修改", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void InsertNewProcess()
        {
            if (skinComboBox9.Text == string.Empty || skinComboBox10.Text == string.Empty || skinComboBox11.Text == string.Empty || skinComboBox12.Text == string.Empty
                || textBoxEx1.Text == string.Empty || textBoxEx2.Text == string.Empty)
            {
                MessageBox.Show("填写信息不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string TeethLocation;
            TeethLocation = textBox1.Text + "," + textBox2.Text + "," + textBox3.Text + "," + textBox4.Text + "," + skinComboBox1.Text + "," + skinComboBox5.Text + ";"
                       + textBox8.Text + "," + textBox7.Text + "," + textBox6.Text + "," + textBox5.Text + "," + skinComboBox2.Text + "," + skinComboBox6.Text + ";"
                       + textBox12.Text + "," + textBox11.Text + "," + textBox10.Text + "," + textBox9.Text + "," + skinComboBox3.Text + "," + skinComboBox7.Text + ";"
                       + textBox16.Text + "," + textBox15.Text + "," + textBox14.Text + "," + textBox13.Text + "," + skinComboBox4.Text + "," + skinComboBox8.Text + ";";

            string sql = string.Format(@"insert into Process(Process_NoteNumber,Patient_ID,Process_Firm,
                                                           Process_SendTime,Process_SenderPhone,Process_GetTime,Process_SenderName,Process_LocationInfo,User_ID,Process_Status,Process_Remark)values(
                                                '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8},'{9}','{10}')",
                                                textBoxEx1.Text,
                                                patientID,
                                                skinComboBox9.Text,
                                                dateTimePicker1.Value.ToString("yyyy-MM-dd"),
                                                textBoxEx2.Text,
                                                dateTimePicker2.Value.ToString("yyyy-MM-dd"),
                                                skinComboBox10.Text,
                                                TeethLocation,
                                                skinComboBox11.SelectedValue,
                                                skinComboBox12.Text,
                                                ucTextBoxEx1.InputText
                                                 );
            DB.Execute(sql);
            MessageBox.Show("保存成功", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Views.Form_ProcessListEdit processListEdit = new Form_ProcessListEdit();
            processListEdit.ListType = 1;
            processListEdit.ShowDialog();
            LoadTypeList();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Views.Form_ProcessListEdit processListEdit = new Form_ProcessListEdit();
            processListEdit.ListType = 2;
            processListEdit.ShowDialog();
            LoadColorList();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Views.Form_ProcessListEdit processListEdit = new Form_ProcessListEdit();
            processListEdit.ListType = 3;
            processListEdit.ShowDialog();
            LoadFirmList();
        }
    }
}
